<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="学校id" prop="schoolId">
      <el-input v-model="dataForm.schoolId" placeholder="学校id"></el-input>
    </el-form-item>
    <el-form-item label="校区id" prop="campusId">
      <el-input v-model="dataForm.campusId" placeholder="校区id"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="学号" prop="studentId">
      <el-input v-model="dataForm.studentId" placeholder="学号"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="studentName">
      <el-input v-model="dataForm.studentName" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="手机号码" prop="phoneNo">
      <el-input v-model="dataForm.phoneNo" placeholder="手机号码"></el-input>
    </el-form-item>
    <el-form-item label="头像链接" prop="photoUrl">
      <el-input v-model="dataForm.photoUrl" placeholder="头像链接"></el-input>
    </el-form-item>
    <el-form-item label="个性签名" prop="personalSignature">
      <el-input v-model="dataForm.personalSignature" placeholder="个性签名"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="registerTime">
      <el-input v-model="dataForm.registerTime" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="上次登录时间" prop="loginTime">
      <el-input v-model="dataForm.loginTime" placeholder="上次登录时间"></el-input>
    </el-form-item>
    <el-form-item label="当前信誉分" prop="creditScore">
      <el-input v-model="dataForm.creditScore" placeholder="当前信誉分"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          schoolId: '',
          campusId: '',
          userName: '',
          studentId: '',
          studentName: '',
          password: '',
          phoneNo: '',
          photoUrl: '',
          personalSignature: '',
          registerTime: '',
          updateTime: '',
          loginTime: '',
          creditScore: ''
        },
        dataRule: {
          schoolId: [
            { required: true, message: '学校id不能为空', trigger: 'blur' }
          ],
          campusId: [
            { required: true, message: '校区id不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          studentId: [
            { required: true, message: '学号不能为空', trigger: 'blur' }
          ],
          studentName: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          phoneNo: [
            { required: true, message: '手机号码不能为空', trigger: 'blur' }
          ],
          photoUrl: [
            { required: true, message: '头像链接不能为空', trigger: 'blur' }
          ],
          personalSignature: [
            { required: true, message: '个性签名不能为空', trigger: 'blur' }
          ],
          registerTime: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          loginTime: [
            { required: true, message: '上次登录时间不能为空', trigger: 'blur' }
          ],
          creditScore: [
            { required: true, message: '当前信誉分不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.schoolId = data.user.schoolId
                this.dataForm.campusId = data.user.campusId
                this.dataForm.userName = data.user.userName
                this.dataForm.studentId = data.user.studentId
                this.dataForm.studentName = data.user.studentName
                this.dataForm.password = data.user.password
                this.dataForm.phoneNo = data.user.phoneNo
                this.dataForm.photoUrl = data.user.photoUrl
                this.dataForm.personalSignature = data.user.personalSignature
                this.dataForm.registerTime = data.user.registerTime
                this.dataForm.updateTime = data.user.updateTime
                this.dataForm.loginTime = data.user.loginTime
                this.dataForm.creditScore = data.user.creditScore
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'schoolId': this.dataForm.schoolId,
                'campusId': this.dataForm.campusId,
                'userName': this.dataForm.userName,
                'studentId': this.dataForm.studentId,
                'studentName': this.dataForm.studentName,
                'password': this.dataForm.password,
                'phoneNo': this.dataForm.phoneNo,
                'photoUrl': this.dataForm.photoUrl,
                'personalSignature': this.dataForm.personalSignature,
                'registerTime': this.dataForm.registerTime,
                'updateTime': this.dataForm.updateTime,
                'loginTime': this.dataForm.loginTime,
                'creditScore': this.dataForm.creditScore
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
